import { WebManageConst } from "src/consts/web_manage_const";
import { AccManageConst } from "src/consts/acc_manage_const";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';

const state: any = {} as any;
// StateIniter.init(state, 'demo');

state.account = {}
state.add_id = ""
state.apply_id = ""
state.apply_name = ""
state.create_date = ""
state.remark = ""
state.id = ""
state.name = ""
state.last_date = ""
state.login_type = ""
state.purpose = ""
state.reason = ""
state.url = ""
state.oldUrl = ""
state.webAccounts = []
state.webConfigs = []
state.webCaMsgs = []
state.oldAccounts = []
state.oldConfigs = []
state.oldCaMsgs = []
state.oldLoginType = ""
state.accountAccounts = []
state.caAndAccountAccounts = []
state.caCaMsgs = []
state.caAndAccountCaMsgs = []
state.accountConfigs = []
state.caConfigs = []
state.caAndAccountConfigs = []
state.web_status = ""
state.config_type = ""
state.config_index = ""
state.acc_type = ""
state.acc_index = ""
state.ca_type = ""
state.ca_index = ""

// vuex

/** 更新数据的 */

function setData(data: any) {
    state.account = data.account
    state.add_id = data.add_id
    state.apply_id = data.apply_id
    state.remark = data.remark
    state.id = data.id
    state.name = data.name
    state.last_date = data.last_date
    state.purpose = data.purpose
    state.reason = data.reason
    state.url = data.url
    state.oldUrl = data.url
    state.web_status = data.web_status
    state.apply_name = data.apply_name
    state.create_date = data.create_date
    state.login_type = data.login_type
    state.oldLoginType = data.login_type
    if (data.login_type == "account" || data.login_type == "ca" || data.login_type == "caAndAccount" || data.login_type == null) {
        if (data.login_type) {
            state.login_key = data.login_type
        } else {
            state.login_key = "account"
        }
    } else {
        state.login_key = "web"
    }

    state.oldAccounts = []
    if (isNoEmpty(data.webAccounts)) {
        for (let i = 0; i < data.webAccounts.length; i++) {
            if (data.webAccounts[i].flag) {
                data.webAccounts[i].shi = "shi"
            } else {
                data.webAccounts[i].shi = "fou"
            }
            state.oldAccounts.push(JSON.parse(JSON.stringify(data.webAccounts[i])))
        }
    }

    state.oldConfigs = []
    if (isNoEmpty(data.webConfigs)) {
        for (let k = 0; k < data.webConfigs.length; k++) {
            if (data.webConfigs[k].must) {
                data.webConfigs[k].shi = "shi"
            } else {
                data.webConfigs[k].shi = "fou"
            }
            state.oldConfigs.push(JSON.parse(JSON.stringify(data.webConfigs[k])))
        }
    }

    state.oldCaMsgs = []
    if (isNoEmpty(data.webAccounts)) {
        for (let j = 0; j < data.webCaMsgs.length; j++) {
            state.oldCaMsgs.push(JSON.parse(JSON.stringify(data.webCaMsgs[j])))
        }
    }

    state.webAccounts = data.webAccounts
    state.webCaMsgs = data.webCaMsgs
    state.webConfigs = data.webConfigs

    state.caAndAccountAccounts = []
    state.caAndAccountConfigs = []
    state.caAndAccountCaMsgs = []
    state.accountAccounts = []
    state.accountConfigs = []
    state.caCaMsgs = []
    state.caConfigs = []
    switch (data.login_type) {
        case "caAndAccount":
            state.caAndAccountAccounts = data.webAccounts
            state.caAndAccountConfigs = data.webConfigs
            state.caAndAccountCaMsgs = data.webCaMsgs
            break;
        case "account":
            state.accountAccounts = data.webAccounts
            state.accountConfigs = data.webConfigs
            break;
        case "ca":
            state.caCaMsgs = data.webCaMsgs
            state.caConfigs = data.webConfigs
            break;
    }

    if (WLStore().state.webManage.type == "add") {
        state.caAndAccountConfigs = [{ "config_type": "account", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "password", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchaimg", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "captcha", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchare", "shi": "fou", "must": true, "handle_type": "click" },
        { "config_type": "login", "shi": "shi", "must": true, "handle_type": "click" }, { "config_type": "forward", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "pin", "shi": "shi", "must": true, "handle_type": "input" }]
        state.accountConfigs = [{ "config_type": "account", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "password", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchaimg", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "captcha", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchare", "shi": "fou", "must": true, "handle_type": "click" },
        { "config_type": "login", "shi": "shi", "must": true, "handle_type": "click" }, { "config_type": "forward", "shi": "shi", "must": true, "handle_type": "input" }]
        state.caConfigs = [{ "config_type": "pin", "shi": "shi", "must": true, "handle_type": "input" }]
    }

    if (data.web_status == "daiyong" && !isNoEmpty(data.webCaMsgs) && !isNoEmpty(data.webAccounts) && !isNoEmpty(data.webConfigs)) {
        if (!data.login_type) {
            state.login_type = "account"
        }

        state.caAndAccountConfigs = [{ "config_type": "account", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "password", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchaimg", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "captcha", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchare", "shi": "fou", "must": true, "handle_type": "click" },
        { "config_type": "login", "shi": "shi", "must": true, "handle_type": "click" }, { "config_type": "forward", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "pin", "shi": "shi", "must": true, "handle_type": "input" }]
        state.accountConfigs = [{ "config_type": "account", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "password", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchaimg", "shi": "shi", "must": true, "handle_type": "input" },
        { "config_type": "captcha", "shi": "shi", "must": true, "handle_type": "input" }, { "config_type": "captchare", "shi": "fou", "must": true, "handle_type": "click" },
        { "config_type": "login", "shi": "shi", "must": true, "handle_type": "click" }, { "config_type": "forward", "shi": "shi", "must": true, "handle_type": "input" }]
        state.caConfigs = [{ "config_type": "pin", "shi": "shi", "must": true, "handle_type": "input" }]
    }

    WLLib.panel(PopupName.WebM_Panel);
}


const mutations = {
    [WebManageConst.CommitWebMShowList](state: any, data: any): void {
        console.log("webM_Panel data update", data);
        var user = getUserInfo()
        var isAdmin = false
        for (let i = 0; i < user.roles.length; i++) {
            if (user.roles[i].id == 1) {
                isAdmin = true
            }
        }

        if (isNoEmpty(data.webCaMsgs) && isAdmin) {
            for (let j = 0; j < data.webCaMsgs.length; j++) {
                data.webCaMsgs[j].uown_name = ""
                var uown_data: any = {}
                uown_data.id = data.webCaMsgs[j].uown_id
                Api.AjaxPost(Config.ajaxUrl + "/account/show.do", uown_data, (res: any) => {
                    if (isNoEmptyObject(res.data)) {
                        data.webCaMsgs[j].uown_name = res.data.name;
                        setData(data)
                    }
                })
            }
        } else {
            setData(data)
        }
    },
    [WebManageConst.CommitConfig](state: any, data: any): void {
        state.config_type = data.config_type
        state.config_index = data.config_index
        WLLib.panel(PopupName.WebM_Config_Panel);
    },
    [WebManageConst.CommitAcc](state: any, data: any): void {
        state.acc_type = data.acc_type
        state.acc_index = data.acc_index
        WLLib.panel(PopupName.WebM_Acc_Panel);
    },
    [WebManageConst.CommitCa](state: any, data: any): void {
        state.ca_type = data.ca_type
        state.ca_index = data.ca_index
        WLLib.panel(PopupName.WebM_Ca_Panel);
    },
    [WebManageConst.CommitConfigData](state: any, data: any): void {
        var type = state.login_type
        if (state.config_type == "edit") {
            var index = state.config_index
            state[type + 'Configs'].splice(index, 1, data);
        } else if (state.config_type == "add") {
            data.ind = state[type + 'Configs'].length
            state[type + 'Configs'].push(data)
        } else if (state.config_type == "up") {
            var index = state.config_index
            if (index > 0) {
                var oldData = JSON.parse(JSON.stringify(state[type + 'Configs'][index - 1]))
                state[type + 'Configs'].splice(index - 1, 1)
                state[type + 'Configs'].splice(index, 0, oldData)
            }
        } else if (state.config_type == "down") {
            var index = state.config_index
            if (index < state[type + 'Configs'].length) {
                var oldData = JSON.parse(JSON.stringify(state[type + 'Configs'][index + 1]))
                state[type + 'Configs'].splice(index + 1, 1)
                state[type + 'Configs'].splice(index, 0, oldData)
            }
        }

        if (!data.isContinue) {
            WLLib.panel(PopupName.WebM_Config_Panel).close();
        }
    },
    [WebManageConst.CommitAccData](state: any, data: any): void {
        var type = state.login_key
        if (state.acc_type == "edit") {
            var index = state.acc_index
            state[type + 'Accounts'].splice(index, 1, data);
        } else if (state.acc_type == "add") {
            state[type + 'Accounts'].push(data)
        }
        if (!data.isContinue) {
            WLLib.panel(PopupName.WebM_Acc_Panel).close();
        }
    },
    [WebManageConst.CommitCaData](state: any, data: any): void {
        var type = state.login_key
        if (state.ca_type == "edit") {
            var index = state.ca_index
            state[type + 'CaMsgs'].splice(index, 1, data);
        } else if (state.ca_type == "add") {
            state[type + 'CaMsgs'].push(data)
        }
        WLLib.panel(PopupName.WebM_Ca_Panel).close();
    },
    [WebManageConst.CommitWebMLogin](state: any, data: any): void {
        state.login_key = data
    }
}

//异步处理
const actions = {
    [WebManageConst.DispatchWebMShow](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/website/show.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(WebManageConst.CommitWebMShowList, res.data);
            }
        })
    },
    [WebManageConst.DispatchWebMEdit](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/website/edit.do", data, (res: any) => {
            WLMessage("编辑网站信息成功", "success")
            let senData: any = {}
            senData["curr_page"] = WLStore().state.webManage.curr_page
            senData["page_size"] = WLStore().state.webManage.page_size
            WLDispatch(WebManageConst.DispatchWebMList, senData);
            WLLib.panel(PopupName.WebM_Panel).close();
        })
    },
    [WebManageConst.DispatchWebMAdd](context: VuexData, data?: any): void {
        var authority = getUserInfo().authority_list
        var req = ""
        var flag = false

        for (let i = 0; i < authority.length; i++) {
            if (authority[i].name == "公共网站管理") {
                for (let j = 0; j < authority[i].child_list.length; j++) {
                    if (authority[i].child_list[j].name == "网站管理") {
                        for (let k = 0; k < authority[i].child_list[j].child_list.length; k++) {
                            if (authority[i].child_list[j].child_list[k].id == "ROLE_website_admin_add") {
                                flag = true
                            }
                        }
                    }
                }
            }
        }

        if (flag) {
            req = "/website/admin/add.do"
        } else {
            req = "/website/normal/add.do"
        }

        Api.AjaxPost(Config.ajaxUrl + req, data, (res: any) => {
            WLMessage("新增网站成功", "success")
            let senData: any = {}
            senData["curr_page"] = WLStore().state.webManage.curr_page
            senData["page_size"] = WLStore().state.webManage.page_size
            WLDispatch(WebManageConst.DispatchWebMList, senData);
            WLLib.panel(PopupName.WebM_Panel).close();
        })
    },
}

export default {
    state,
    mutations,
    actions
}